<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h2 class="login-title">轻客管家</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="login-buttons">
            <el-button type="primary" @click="handleLogin" :loading="loading">登录</el-button>
            <el-button @click="handleReset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'LoginView',
  setup() {
    const loginFormRef = ref(null)
    const loading = ref(false)
    const errorMsg = ref('')
    
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    // 表单验证规则
    const rules = reactive({
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    })
    
    // 处理登录
    const handleLogin = async () => {
      loading.value = true
      
      try {
        // 调用后端登录接口
        const response = await fetch('/api/login', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(loginForm)
        })
        
        const result = await response.json()
        
        if (result.msg === 'success' && result.data) {
          // 登录成功，保存用户信息和token到localStorage
          localStorage.setItem('user', JSON.stringify(result.data))
          localStorage.setItem('token', result.data.token)
          ElMessage.success('登录成功')
          // 跳转到首页
          window.location.href = '/'
        } else {
          ElMessage.error(result.msg || '登录失败')
        }
      } catch (error) {
        console.error('登录请求错误:', error)
        ElMessage.error('登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
    
    // 处理重置
    const handleReset = () => {
      loginForm.username = ''
      loginForm.password = ''
      errorMsg.value = ''
    }
    
    return {
      loginForm,
      loginFormRef,
      loading,
      rules,
      handleLogin,
      handleReset
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  width: 300px;
  height: 300px;
  background: radial-gradient(circle, rgba(193,218,255,0.4) 0%, rgba(255,255,255,0) 70%);
  top: 10%;
  left: 10%;
  border-radius: 50%;
}

.login-container::after {
  content: '';
  position: absolute;
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(193,218,255,0.3) 0%, rgba(255,255,255,0) 70%);
  bottom: 10%;
  right: 10%;
  border-radius: 50%;
}

.login-form-wrapper {
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
  color: #1f2937;
  font-weight: 500;
}

.login-buttons {
  display: flex;
  justify-content: space-between;
}

.login-buttons .el-button {
  width: 48%;
  padding: 10px 0;
}



.el-form-item__label {
  color: #606266;
}

.el-input {
  width: 100%;
}

@media (max-width: 768px) {
  .login-form-wrapper {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>